
package rendi456.structure.list;

import java.util.Random;

class ListeDemo
{
	private static final int N = 50;

	public static void faireDemo()
	{
		// ListeDemo.ajouterDebutDemo();
		// ListeDemo.ajouterFinDemo();
		// ListeDemo.enleverFinDemo();
		// ListeDemo.enleverElementDemo();
		// ListeDemo.enleverElementDemo2();
		// ListeDemo.toTableauDemo();
		// ListeDemo.enleverDebutDemo();
		ListeDemo.setElementDemo();
	}

	public static void ajouterDebutDemo()
	{
		Liste<Integer> list = new Liste<Integer>();

		System.out.println("\nDemo ajouterDebut");
		for (int i = 0; i < ListeDemo.N; i++)
		{
			list.ajouterDebut(new Integer(i));
		}
		System.out.println(list.toString());
	}

	public static void ajouterFinDemo()
	{
		Liste<Integer> list = new Liste<Integer>();

		System.out.println("\nDemo ajouterFin");
		for (int i = 0; i < ListeDemo.N; i++)
		{
			list.ajouterFin(new Integer(i));
		}
		System.out.println(list.toString());
	}

	public static void enleverFinDemo()
	{
		Liste<Integer> list = new Liste<Integer>();
		Integer buffer = null;

		System.out.println("\nDemo enleverFin");
		for (int i = 0; i < ListeDemo.N; i++)
		{
			list.ajouterFin(new Integer(i));
		}
		System.out.println(list.toString());

		for (int i = 0; i < ListeDemo.N; i++)
		{
			buffer = list.enleverFin();
			System.out.print("\n" + "retour = " + buffer.toString() + " | ");
			System.out.println(list.toString());
		}
	}

	public static void enleverElementDemo()
	{
		Random rand = new Random();
		Liste<Integer> list = null;
		Integer buffer = null;
		Integer cible = null;
		boolean succes = false;

		System.out.println("\nenleverElementDemo");
		list = new Liste<Integer>();

		for (int i = 0; i < ListeDemo.N; i++)
		{
			if (rand.nextBoolean())
			{
				buffer = new Integer(i);

			}
			else
			{
				buffer = null;
			}

			list.ajouterFin(buffer);
		}

		System.out.println("liste = " + list.toString());

		while (!list.estVide())
		{
			if (rand.nextBoolean())
			{
				cible = null;
			}
			else
			{
				cible = rand.nextInt(ListeDemo.N);
			}

			succes = list.enleverElement(cible);
			System.out.println("\nc = " + cible + " | s = " + succes);
			System.out.println(list.toString());
		}
	}

	public static void suffle(Liste<Integer> liste)
	{
		Random ran = new Random();
		Liste<Integer> listeTemp = new Liste<Integer>();
		Integer buffer = null;

		while (!liste.estVide())
		{
			buffer = liste.enleverFin();
			if (ran.nextFloat() > 0.5f)
			{
				listeTemp.ajouterFin(buffer);
			}
			else
			{
				listeTemp.ajouterDebut(buffer);
			}
		}

		while (!listeTemp.estVide())
		{
			liste.ajouterDebut(listeTemp.enleverFin());
		}
	}

	public static void enleverDebutDemo()
	{
		Liste<Integer> list = new Liste<Integer>();
		Integer buffer = null;
		System.out.println("\nenleverDebutDemo");
		for (int i = 0; i < ListeDemo.N; i++)
		{
			list.ajouterFin(new Integer(i));
		}
		System.out.println(list.toString());

		while (!list.estVide())
		{
			buffer = list.enleverDebut();
			System.out.println("\nretour = " + buffer.toString());
			System.out.println(list.toString());
		}
	}

	public static void enleverElementDemo2()
	{
		Random rand = new Random();
		Integer buffer = null;
		Liste<Integer> list = new Liste<Integer>();
		int cible = 0;
		int nb = ListeDemo.N * 1;

		System.out.println("\nenleverElementDemo2");
		for (int i = 0; i < nb; i++)
		{
			list.ajouterFin(new Integer(i));
		}
		System.out.println(list.toString());

		for (int i = nb; i >= 1; i--)
		{
			cible = rand.nextInt(i);
			buffer = list.enleverElement(cible);
			System.out.println("\nenlever " + cible + " | retour = " + buffer);
			System.out.println(list.toString());
		}
	}

	public static void toTableauDemo()
	{
		Object[] tabl = null;
		String out = "";
		System.out.println("\ntoTableauDemo");
		Liste<Integer> list = new Liste<Integer>();

		for (int i = 0; i < ListeDemo.N; i++)
		{
			list.ajouterFin(new Integer(i));
		}
		System.out.println(list.toString());

		tabl = list.toTableau();

		System.out.println("resultat");
		for (int i = 0; i < tabl.length; i++)
		{
			out += tabl[i].toString() + ", ";
		}
		System.out.println(out);
	}

	public static void setElementDemo()
	{
		Random rand = new Random();
		Integer buffer = null;
		Liste<Integer> list = new Liste<Integer>();

		System.out.println("\nsetElementDemo");

		for (int i = 0; i < ListeDemo.N; i++)
		{
			list.setElement(new Integer(i), 10);
			System.out.println("\n" + list.toString());
		}

	}

	public static void main(String[] args)
	{
		faireDemo();
	}
}
